package com.example.knife4jdemo.controller;

import com.example.knife4jdemo.beans.UserEntity;
import com.example.knife4jdemo.config.ResponseDataDiy;
import com.github.xiaoymin.knife4j.annotations.*;
import com.google.common.collect.Lists;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 建议优先级是：@ApiSupport > @ApiSort > @Api
 */
@Api(tags = {"后台-用户模块"})
@ApiSort(value = 100)// 大分类顺序
//@ApiSupport(order = 10)
@RestController
@RequestMapping("/user")
public class SysUserController {

    // 优先级：position > @ApiOperationSort   https://www.bookstack.cn/read/knife4j/enh-func.md
    @ApiOperation(value = "test0-获取用户信息接口", notes = "说明1", tags = "查单个")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int")
    @PostMapping("/postMember")
    @ApiOperationSupport(order = 1)
    public UserEntity postMember(@RequestParam Integer id) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setName("admin");
        return userEntity;
    }

    @ApiOperation(value = "test01-findByUserId接口", notes = "根据用户id查询用户信息", tags = "查单个")
    @ApiImplicitParam(name = "id", value = "用户id", paramType = "path")
    @GetMapping("/findByUserId/{id}")
    @ApiOperationSupport(order = 2)
    public ResponseDataDiy<UserEntity> findByUserId(@PathVariable("id") Integer id) {
        UserEntity userVO = new UserEntity(id, "Jaemon");

        return new ResponseDataDiy(0, "成功", userVO);
    }

    @ApiOperation(value = "test3-添加用户1", nickname = "添加用户接口1", notes = "入参是复杂对象", produces = "application/json")
    @PostMapping("/postUser")
    @ResponseBody
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true, dataType = "int")
    @ApiOperationSupport(order = 3)
    public UserEntity postUser(@RequestBody UserEntity user, @RequestParam("userId") int userId) { // 这里用包装类竟然报错
        if (user.getId() == userId) {
            return user;
        }
        return new UserEntity();
    }


    @ApiOperation(value = "test4-添加用户2", nickname = "添加用户接口2", notes = "入参是简单对象", produces = "application/json")
    @PostMapping("/addUser")
    @ResponseBody
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户姓名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "int")})
    public UserEntity addUser(String userName, int id) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName(userName);
        userEntity.setId(id);
        return userEntity;
    }

    @ApiOperation(value = "test5-添加用户3", nickname = "添加用户接口3", notes = "入参是RequestBody", produces = "application/json")
    @PostMapping("/addUser3")
//    @ApiOperationSupport(ignoreParameters = {"user.id","user.name","user.orderDate.id"})
    @ResponseBody
    @ApiOperationSupport(order = 5)
    public UserEntity addUser3(@RequestBody UserEntity user) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName(user.getName());
        userEntity.setId(user.getId());
        return userEntity;
    }


    @ApiOperation(value = "test6-入参为map测试", produces = "application/json")
    @PostMapping("/test4")
//    @ApiOperationSupport(ignoreParameters = {"user.id","user.name","user.orderDate.id"})
    @ResponseBody
    @ApiOperationSupport(order = 6)
    @DynamicParameters(name = "CreateOrderHashMapModel", properties = {
            @DynamicParameter(name = "", value = "注解id", example = "X000111", required = true, dataTypeClass = Integer.class),
            @DynamicParameter(name = "name3", value = "订单编号-gson"),
            @DynamicParameter(name = "name1", value = "订单编号1-gson"),
    })
    public ResponseDataDiy<HashMap> createOrder1235332(@RequestBody HashMap map) {
        ResponseDataDiy<HashMap> r = new ResponseDataDiy();
        r.setData(map);
        return r;
    }


    /**
     * 多层级文档返回结果说明
     * <p>
     * 多层级返回参数为了更好展示返回数据参数类型需要额外增加doc相关类
     */
    @ApiOperation(value = "test7-多层级返回", produces = "application/json")
    @ApiOperationSupport(
            params = @DynamicParameters(name = "createModel02", properties = {
                    @DynamicParameter(name = "id", value = "注解id", example = "X000111", required = true, dataTypeClass = Integer.class),
                    @DynamicParameter(name = "name", value = "注解名称")
            }),
            responses = @DynamicResponseParameters(name = "ResponseData", properties = {
                    @DynamicParameter(name = "code", value = "状态码", example = "0"),
                    @DynamicParameter(name = "msg", value = "状态描述", example = "success"),
                    @DynamicParameter(name = "data", value = "结果信息", dataTypeClass = UserEntity.class)
            }),
            order = 7
    )
    @PostMapping("/create_02")
    public ResponseEntity<String> create_02(@RequestBody Map map) {
        return ResponseEntity.ok("success");
    }


    /**
     * https://zhuanlan.zhihu.com/p/109048410
     * 单层级文档说明，一般业务参考此处即可
     * <p>
     * ①query参数
     * ②body参数说明
     * ③url路径参数（不建议使用）
     * <p>
     * query 参数说明：实际开发中删减下面代码中的字段属性
     * body  参数说明：此方法对单层传输参数使用，一般业务情况足够使用
     * url   参数说明：url携带参数不建议使用[url协调不好会导致越权问题]
     */
    @ApiOperation(value = "test8-jdk-Map-动态创建显示参数", produces = "application/json")
    @ApiImplicitParams(value = {
            /*query参数*/
            @ApiImplicitParam(name = "id", value = "注解id", required = true, dataTypeClass = String.class, paramType = "query", example = "000001"),
            @ApiImplicitParam(name = "name", value = "注解名称", required = true, dataTypeClass = Integer.class, paramType = "query", example = "mk567"),
            /*url参数*/
            @ApiImplicitParam(name = "mk", value = "注解名称", required = true, dataTypeClass = Integer.class, paramType = "path", example = "1234565")
    })
    /*body参数*/
    @ApiOperationSupport(
            params = @DynamicParameters(name = "createModel01", properties = {
                    @DynamicParameter(name = "id", value = "注解id", example = "X000111", required = true, dataTypeClass = Integer.class),
                    @DynamicParameter(name = "name", value = "注解名称")
            }),
            responses = @DynamicResponseParameters(name = "CreateOrderMapModel", properties = {
                    @DynamicParameter(name = "id", value = "注解id", example = "X000111", required = true, dataTypeClass = Integer.class),
                    @DynamicParameter(name = "name", value = "注解名称")
            }),
            order = 8
    )
    @PostMapping("/create_01/{mk}")
    public ResponseEntity<Map> create_01(@RequestBody Map map, @PathVariable(value = "mk") String mk) {
        System.out.println(mk);
        return ResponseEntity.ok(map);
    }

    /*返回值的备注说明*/
    @PostMapping("/test9_userList")
    @ApiOperation(value = "test9-返回值的备注说明", notes = "查询用户列表")
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "请求成功"),
            @ApiResponse(code = 1, message = "请求失败")
    })
    @ApiOperationSupport(order = 9)
    public ResponseDataDiy userList(@RequestBody UserEntity userReqDTO) {
        List<UserEntity> userVOS = Lists.newArrayList();
        UserEntity userVO;
        for (int i = 0; i < 5; i++) {
            userVO = new UserEntity(i, "Jaemon" + i);
            userVOS.add(userVO);
        }
        return new ResponseDataDiy(0, "成功", userVOS);
    }


}
